﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LogCMS.DAL;
using System.ComponentModel;
using Portal.Core.DAL;


namespace Portal.BO.Editoral.Statistic
{
    public static class StatContentHelper
    {
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static DataTable GetNewslist(string strWhere, string isFirstLoad, int PageSize, int StartRow)
        {
            //Lay gia tri mode list de xu ly
            if (strWhere == null)
                strWhere = "";
            if (strWhere.Trim() != "")
            {
                strWhere = " Where " + strWhere;
            }
            if (isFirstLoad.ToLower() == "true")
                StartRow = 0;

            DataTable objresult;
            int intPageNum = StartRow / PageSize + 1;
            //lay duoc du lieu cua tat ca nhung thang co trang thai la status can lay
            using (Portal.Core.DAL.MainDB objdb = new Portal.Core.DAL.MainDB())
            {
                //objresult = objdb.NewsCollection.GetPageAsDataTable(intPageNum, PageSize, strWhere, "News_ModifiedDate DESC"); ;
                int startIndex = (intPageNum  -1)* PageSize;
                int endIndex = startIndex + PageSize;
                objresult = objdb.StoredProcedures.News_GetListByWhereForPaging(strWhere, startIndex.ToString(), endIndex.ToString());
                
            }

            return objresult;
        }


        public static DataTable GetNewslistFroComment(string strWhere, string isFirstLoad, int PageSize, int StartRow)
        {
            //Lay gia tri mode list de xu ly
            if (strWhere == null)
                strWhere = "";
            if (strWhere.Trim() != "")
            {
                strWhere = " Where " + strWhere;
            }
            if (isFirstLoad.ToLower() == "true")
                StartRow = 0;

            DataTable objresult;
            int intPageNum = StartRow / PageSize + 1;
            //lay duoc du lieu cua tat ca nhung thang co trang thai la status can lay
            using (Portal.Core.DAL.MainDB objdb = new Portal.Core.DAL.MainDB())
            {
                //objresult = objdb.NewsCollection.GetPageAsDataTable(intPageNum, PageSize, strWhere, "News_ModifiedDate DESC"); ;
                int startIndex = (intPageNum - 1) * PageSize;
                int endIndex = startIndex + PageSize;
                objresult = objdb.StoredProcedures.News_GetListByWhereForPaging(strWhere, startIndex.ToString(), endIndex.ToString());

            }
            if (objresult.Rows.Count == 0)
                return objresult;

            objresult.Columns.Add(new DataColumn("CountComment"));
            DataTable objTemp = objresult.Clone();
            object[] objarr = null;

            Portal.Core.DAL.MainDB objdb1 = new Portal.Core.DAL.MainDB();
            foreach (DataRow objrow in objresult.Rows)
            {
                objarr = objrow.ItemArray;
                objarr[objarr.Length - 1] = objdb1.StoredProcedures.CountCommentByNewsId(objrow["News_ID"].ToString()).Rows[0][0];
                objTemp.Rows.Add(objarr);
            }
            objdb1.Close();

            objresult.Dispose();
            return objTemp;
        }
       
        /// <summary>
        /// Ham lay Action hien thoi cua tin
        /// </summary>
        /// <param name="_news_id">ID cua tin</param>
        /// <returns></returns>

        public static int GetRowsCount(string strWhere, string isFirstLoad)
        {
            if (strWhere == null)
                strWhere = "";
            if (strWhere.Trim() != "")
            {
                strWhere = " Where " + strWhere;
            }

            DataTable objresult;
            using (Portal.Core.DAL.MainDB objdb = new Portal.Core.DAL.MainDB())
            {
                objresult = objdb.StoredProcedures.vc_Execute_Sql("Select Count(*) From News " + strWhere);
            }
            return Convert.ToInt32(objresult.Rows[0][0]);
        }
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static DataTable getPage(int numPage)
        {
            int intPagenum = numPage;
            DataTable objTb = new DataTable();
            objTb.Columns.Add(new DataColumn("Text", typeof(string)));
            objTb.Columns.Add(new DataColumn("Value", typeof(string)));
            for (int i = 1; i <= intPagenum; i++)
            {
                DataRow myRow = objTb.NewRow();
                myRow["Text"] = i.ToString();
                myRow["Value"] = Convert.ToString(i - 1);
                objTb.Rows.Add(myRow);
            }
            if (intPagenum  == 0)
            {
                DataRow myRow = objTb.NewRow();
                myRow["Text"] = "1";
                myRow["Value"] = "0";
                objTb.Rows.Add(myRow);
            }
            return objTb;
        }

        public static DataTable GetListStatisticContentHour(string news_id)
        {
            DataTable dt = new DataTable();
            if (news_id.Trim() != "")
            { 
                using(ReportingDB objReportingDB = new ReportingDB())
                {
                    dt = (DataTable)objReportingDB.CallStoredProcedure("GetStatisticHourContent", new object[] { Convert.ToInt64(news_id.Trim()) }, new string[] { "@article_id" }, true);
                }
                
            }
            return dt;
        }


        
    }



    public class ReportingDB : Portal.Core.DAL.MainDB_Base
    {

        public ReportingDB()
        {

        }
        public ReportingDB(bool init)
            : base(init)
        {

        }
        /// <summary>
        /// Creates a new connection to the database.
        /// </summary>
        /// <returns>An <see cref="System.Data.IDbConnection"/> object.</returns>
        protected override IDbConnection CreateConnection()
        {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["reporting"].ToString());
            return con;
        }
        /// <summary>
        /// Returns a SQL statement parameter name that is specific for the data provider.
        /// For example it returns ? for OleDb provider, or @paramName for MS SQL provider.
        /// </summary>
        /// <param name="paramName">The data provider neutral SQL parameter name.</param>
        /// <returns>The SQL statement parameter name.</returns>
        protected override string CreateSqlParameterName(string paramName)
        {
            return "@" + paramName;
        }

        /// <summary>
        /// Creates a .Net data provider specific parameter name that is used to
        /// create a parameter object and add it to the parameter collection of
        /// <see cref="System.Data.IDbCommand"/>.
        /// </summary>
        /// <param name="baseParamName">The base name of the parameter.</param>
        /// <returns>The full data provider specific parameter name.</returns>
        protected override string CreateCollectionParameterName(string baseParamName)
        {
            return "@" + baseParamName;
        }

        public DataTable vc_Execute_Sql(String sSql)
        {
            IDbCommand cmd = CreateCommand("vc_Execute_Sql", true);
            AddParameter(cmd, "sSql", DbType.String, sSql);
            DataTable table = CreateDataTable(cmd);
            return table;
        }
    }
}
